package com.tyylab.message.distribute.message;

import com.alibaba.fastjson.JSON;
import com.tyylab.message.api.event.model.PingEvent;
import com.tyylab.message.core.intf.IConditionalSubscribe;
import com.tyylab.message.core.model.TransMessage;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class SimulEventSubscribe implements IConditionalSubscribe {

    @Autowired
    SimulEventRepublish republish;

    @Override
    public boolean isSubscribe(String clz) {
        if (StringUtils.isNotBlank(clz) && !StringUtils.equalsIgnoreCase(PingEvent.class.getName(), clz)) {
            return true;
        }
        else{
            return false;
        }
    }

    @SneakyThrows
    @Override
    public void onMesssage(TransMessage simulMessage) {
        MDC.put("requestId",simulMessage.getId());
        log.info("process message : " + JSON.toJSONString(simulMessage));
        republish.republish(simulMessage);
        log.info("finish process message : " +JSON.toJSONString(simulMessage));
    }

    public static void main(String[] args) {
        System.out.println(new SimulEventSubscribe().isSubscribe("com.yk.basic.platform.common.event.common.PingEvent"));
    }
}
